<?php
class Training_Model_UserGroup extends Zend_Db_Table{
	protected $_name = 'user_group';
	protected $_primary = 'id';
	
	public function testItem($arrParam = null, $options = null){
		$db = $this->getAdapter();
		$sql = 'SELECT * FROM user_group';
		$result = $db->fetchAll($sql);
		echo '<pre>';
		print_r($result);
		echo '</pre>';
	}
	
	public function getItem($arrParam = null, $options = null){
		if($options == null){
			$where = 'id < 14';
			$result = $this->fetchRow($where, 'id DESC',1)->toArray();
		}
		
		return $result;
	}
	
	public function saveItem($arrParam = null, $options = null){
		if($options == null){
			$data = array(
							'name'			=> "ABC 123 group",
							'avatar'		=> "avatar.png",
							'ranking'		=> "ranking.png",
							'group_acp'		=> 0,
							'group_default'	=> 0,
							'created'		=> date("Y-m-d H:i:s"),
							'created_by'	=> 1,
							'permission'	=> "Limit Access",
							'status'		=> 1,
							);
			$row = $this->createRow($data);
			$row->save();
			echo '<br>' . $row->id;
			/*echo '<pre>';
			print_r($row);
			echo '</pre>';*/
		}
		
		if($options['task'] == 'fetch'){
			$row = $this->fetchNew();
			$row->name 			= 'FGH 123 group';
			$row->avatar 		= 'avatar.png';
			$row->ranking 		= 'ranking.png';
			$row->group_acp 	= 0;
			$row->group_default = 0;
			$row->created 		= date("Y-m-d H:i:s");
			$row->created_by 	= 1;
			$row->save();
			echo '<br>' . $row->id;
			/*echo '<pre>';
			print_r($row);
			echo '</pre>';*/
			
		}
		
		if($options['task'] == 'fetch2'){
			$where = 'id = 45';
			$row = $this->fetchRow($where);
			$row->name = 'Queen group';
			$row->order = 5;
			$row->save();
			
			echo '<pre>';
			print_r($row);
			echo '</pre>';
			
		}
		
		if($options['task'] == 'insert'){
			$data = array(
							'name'			=> "ABC 123 group",
							'avatar'		=> "avatar.png",
							'ranking'		=> "ranking.png",
							'group_acp'		=> 0,
							'group_default'	=> 0,
							'created'		=> date("Y-m-d H:i:s"),
							'created_by'	=> 1,
							'permission'	=> "Limit Access",
							'status'		=> 1,
							);
			$this->insert($data);
			
			
		}
		
		if($options['task'] == 'update'){
			$data = array(
							'name'			=> "123 ABC group",
							'avatar'		=> "avatar123.png",
							'ranking'		=> "ranking123.png",
							
							);
			$where = "name = 'ABC 123 group'";						
			$this->update($data, $where);
			
			
		}
	}
	
	public function deleteItem($arrParam = null, $options = null){
		if($options == null){
			$where = 'id = 36';
			$this->delete($where);
		}
		
		if($options['task'] == 'multi'){
			$where = 'id IN (35,33,34)';
			$this->delete($where);
		}
	}
	
	public function listItem($arrParam = null, $options = null){
		echo '<br>' . __METHOD__;
		
		echo '<pre>';
		print_r($arrParam);
		echo '</pre>';
		
		echo '<pre>';
		print_r($options);
		echo '</pre>';
		
		if($options == null){
			$result = $this->fetchAll()->toArray();
		}
		
		if($options['task'] == 'group-default'){
			$where = 'group_default = 1 AND group_acp = 1';
			
			$result = $this->fetchAll($where)->toArray();
		}
		
		if($options['task'] == 'group-default-2'){
			echo '<br>' . $where = 'group_default = ' . $arrParam['default'] . ' AND group_acp = ' . $arrParam['acp'];
			
			$result = $this->fetchAll($where)->toArray();
		}
		
		if($options['task'] == 'order'){
			echo '<br>' . $order = 'id ASC';
			
			$result = $this->fetchAll(null,$order)->toArray();
		}
		
		if($options['task'] == 'limit'){			
			
			$result = $this->fetchAll(null,null,5,1)->toArray();
		}
		
		return $result;
	}
	
	private function listItemGroupDefault(){
		
	}
}